function request(url, params, method, success, fail) {
  this.requestLoading(url, params, method, "", success, fail)
}
// 展示进度条的网络请求
// url:网络请求的url
// params:请求参数
// message:进度条的提示信息
// success:成功的回调函数
// fail：失败的回调

//get
function requestLoading(url, params, method, message, success, fail) {
  console.log(params)
  wx.showNavigationBarLoading()
  if (message != "") {
    wx.showLoading({
      title: message,
    })
  }
  wx.request({
    url: url,
    data: params,
    method: method,
    header: {
      'Content-Type': 'application/json'
      //'content-type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
      //console.log(res.data)
      wx.hideNavigationBarLoading()
      if (message != "") {
        wx.hideLoading()
      }
      if (res.statusCode == 200) {
        success(res.data)
      } else {
        fail()
      }
    },
    fail: function (res) {
      wx.hideNavigationBarLoading()
      if (message != "") {
        wx.hideLoading()
      }
      fail()
    },
    complete: function (res) {
    },
  })
}

//post
function requestPost(url, params, method, message, success, fail) {
  console.log(params)
  wx.showNavigationBarLoading()
  if (message != "") {
    wx.showLoading({
      title: message,
    })
  }
  wx.request({
    url: url,
    data: params,
    method: method,
    header: {
      //'Content-Type': 'application/json'
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
      //console.log(res.data)
      wx.hideNavigationBarLoading()
      if (message != "") {
        wx.hideLoading()
      }
      if (res.statusCode == 200) {
        success(res.data)
      } else {
        fail()
      }
    },
    fail: function (res) {
      wx.hideNavigationBarLoading()
      if (message != "") {
        wx.hideLoading()
      }
      fail()
    },
    complete: function (res) {

    },
  })
}

//server
function server() {
  var url = 'https://api-tbyl.moqilin.net/'
  return url;
}

//转时间戳
function appTime(ordertime) {
  var date = new Date(ordertime * 1000);
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
  var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
  var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + '';
  return Y + M + D + h + m;
}
//上传文件
function upload(url,page, path) {
  wx.showToast({
    icon: "loading",
    title: "正在上传请稍后",
    duration: 4000
    
  }),
  wx.uploadFile({
    url: url,
    filePath: path[0],
    name: 'file',
    header: { "Content-Type": "multipart/form-data" },
    formData: {},
    success: function (res) {
      console.log(res.data);
      var json = JSON.parse(res.data);
      var imagePath = json.imagePath;
      page.setData({  //上传成功修改显示头像
        src: path[0],
        imagePath: imagePath
      })
      console.log(page.data.imagePath)
      if (json.status === false) {
        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        })
        return;
      }
      else if (json.success == true) {
        wx.showModal({
          title: '提示',
          content: '上传成功',
          showCancel: false
        })
        return;
      }

    },
    fail: function (e) {
      console.log(e);
      wx.showModal({
        title: '提示',
        content: '上传失败',
        showCancel: false
      })
    },
    complete: function () {
      wx.hideToast();  //隐藏Toast
    }
  })
}
module.exports = {
  request: request,
  requestLoading: requestLoading,
  requestPost: requestPost,
  appTime: appTime,
  server: server,
  upload: upload
}
